const app = getApp();
Component({
    properties: {},
    data: {
        status: ['正常', '异常'],
        showDate: false,
        date: '选择日期',
        // y-m-d
        startTimestamp: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate(),
        endTimestamp: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate(),

        minDate: new Date(2022, 1, 1).getTime(),
        maxDate: new Date().getTime(),


        token: '3U0fClbRjIwu5c5QxVkP',
        historyList: []
    },

    lifetimes: {
        attached: function () {
            this.setToken();
            if (this.data.token != '') {
                this.getHistoryList();
            } else {
                wx.showToast({
                    title: '请先登录',
                    icon: 'none',
                    duration: 2000
                })
            }

        }
    },
    methods: {

        setToken() {
            this.setData({
                token: app.globalData.token
            })
            console.log("this.data.toke")
            console.log(this.data.token)
        },

        formatData: function (date) {
            return data.toFixed(4);
        },


        getHistoryList() {
            wx.request({
                url: 'http://43.143.141.18:8081/EnvData/getEnvData/' + this.data.startTimestamp + '/' + this.data.endTimestamp + '/' + this.data.token,
                method: 'GET',
                success: (res) => {
                    console.log(res)
                    this.setData({
                        historyList: res.data
                    })
                    console.log(this.data.historyList)
                },
                fail: (res) => {
                    console.log(res)
                },
            })
        },


        selectDate(e) {
            console.log(e);
            this.onDisplay()

        },
        onDisplay() {
            this.setData({showDate: true});
        },
        onClose() {
            this.setData({showDate: false});
        },
        formatDate(date) {
            date = new Date(date);
            return `${date.getMonth() + 1}/${date.getDate()}`;
        },
        onConfirm(event) {
            const [start, end] = event.detail;
            this.setData({
                showDate: false,
                date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
                startTimestamp: start.getFullYear() + '-' + (start.getMonth() + 1) + '-' + start.getDate(),
                endTimestamp: end.getFullYear() + '-' + (end.getMonth() + 1) + '-' + end.getDate(),
            });
            this.getHistoryList();
        },
    }
});
